local skill = fk.CreateSkill{
  name = "tea__lianfang"
}
local U = require "packages/utility/utility"
---@type TrigSkelSpec<TrigFunc>
local lianfang = {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = #player:getCardIds(player.Equip) + 1
    local targets = table.filter(room.alive_players, function(p)
      return not p:isNude()
    end)
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = num,
      skill_name = skill.name,
      prompt = "#tea__lianfang-choose:::"..num
    })
    if #tos > 0 then
      event:setCostData(self, tos)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self)
    local extraData = {
      num = 1,
      min_num = 1,
      include_equip = true,
      pattern = ".",
      reason = self.name,
    }
    local prompt = "#askForLianFang"
    local dat = { "choose_cards_skill", prompt, false, extraData }
    local req = Request:new(targets, "AskForUseActiveSkill")
    req.focus_text = "#tea__lianfang_considering"
    for _, to in ipairs(targets) do
      req:setData(to, dat)
    end

    local cards = {}
    for _, p in ipairs(targets) do
      local cids
      if p.reply_ready then
        local replyCard = req:getResult(p).card
        cids = table.random(replyCard.subcards, 1)
      else
        cids = table.random(p:getCardIds(Player.Hand), 1)
      end
      table.insertIfNeed(cards, cids[1])
      room:recastCard(cids, p)
    end
    if #cards > 0 then
      local types = {}
      if table.find(cards, function(id)
        return Fk:getCardById(id).type == Card.TypeBasic
      end) then
        table.insert(types, "basic")
      end
      if table.find(cards, function(id)
        return Fk:getCardById(id).type == Card.TypeTrick
      end) then
        table.insert(types, "trick")
      end
      if table.find(cards, function(id)
        return Fk:getCardById(id).type == Card.TypeEquip
      end) then
        table.insert(types, "equip")
      end
      local result = U.askforViewCardsAndChoice(player, cards, types, self.name, "#tea__lianfang-gain")
      if result then
        local dic = {basic = Card.TypeBasic, trick = Card.TypeTrick, equip = Card.TypeEquip}
        local type_c = dic[result]
        room:obtainCard(player, table.filter(cards, function(id)
          return Fk:getCardById(id).type == type_c
        end), true, fk.ReasonPrey)
      end
    end
  end,
}
skill:addEffect(fk.EventPhaseStart, lianfang)
return skill